package com.ruoyi.quality.controller.web;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quality.domain.*;
import com.ruoyi.quality.service.IMessagePushService;
import com.ruoyi.quality.service.ITQualityQuestionsService;
import com.sun.java.swing.plaf.windows.WindowsInternalFrameTitlePane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/*
* 首页推送信息接口 lxg
* */
@RestController
@RequestMapping("/quality/messagePush")
public class MessagePushController extends BaseController {
    @Autowired
    private IMessagePushService iMessagePushService;

    /**
     * 查询待处理信息
     */
    @GetMapping("/list")
    public List list(Long projectId)
    {
        List<TMessagePushFile> list = new ArrayList<>();
        try{
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if(loginUser!=null){
                TMessagePushFile dealData  = iMessagePushService.getPushMessageDeal(loginUser.getUserId(),projectId);

                TMessagePushFile auditData  = iMessagePushService.getPushMessageAudit(loginUser.getUserId(),projectId);

                TMessagePushFile ApprovalData  = iMessagePushService.getPushMessageApproval(loginUser.getNickname(),projectId);

                if(Integer.parseInt(dealData.getPushCount())>0){
                    dealData.setPushType("dcl");
                    dealData.setPushName("待整改记录");
                    list.add(dealData);
                }
                if(Integer.parseInt(auditData.getPushCount())>0){
                    auditData.setPushType("dsh");
                    auditData.setPushName("待复查记录");
                    list.add(auditData);
                }

                if(Integer.parseInt(ApprovalData.getPushCount())>0){
                    ApprovalData.setPushType("yqsh");
                    ApprovalData.setPushName("延期待审记录");
                    list.add(ApprovalData);
                }

            }
        }catch (Exception e){

        }
        return list;
    }
    /*手机端推送接口*/
    @GetMapping("/api/list")
    public JSONObject apiList(Long projectId)
    {
        JSONObject j = new JSONObject();
        try{
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if(loginUser!=null){
                TMessagePushFile dealData  = iMessagePushService.getPushMessageDeal(loginUser.getUserId(),projectId);

                TMessagePushFile auditData  = iMessagePushService.getPushMessageAudit(loginUser.getUserId(),projectId);

                TMessagePushFile ApprovalData  = iMessagePushService.getPushMessageApproval(String.valueOf(loginUser.getUserId()),projectId);

                if(Integer.parseInt(dealData.getPushCount())>0){
                    dealData.setPushType("dcl");
                    dealData.setPushName("待整改记录");
                    j.put("dealData",dealData);
                }
                if(Integer.parseInt(auditData.getPushCount())>0){
                    auditData.setPushType("dsh");
                    auditData.setPushName("待复查记录");
                    j.put("auditData",auditData);
                }

                if(Integer.parseInt(ApprovalData.getPushCount())>0){
                    ApprovalData.setPushType("yqsh");
                    ApprovalData.setPushName("延期待审记录");
                    j.put("ApprovalData",ApprovalData);
                }
                j.put("code",200);
                j.put("message","查询成功");
            }
        }catch (Exception e){
            j.put("code",500);
            j.put("message","查询失败");
        }
        return j;
    }
    /**
     * 按照统计质量问题超期数量
     */
    @GetMapping("/listProjectOverTime")
    public JSONObject listProjectOverTime(TIndexData tIndexData){
        JSONObject jo = new JSONObject();
        List<TIndexData> list = iMessagePushService.listProjectOverTime(tIndexData);
        jo.put("data",list);
        return jo;
    }

    /**
     * 统计问题来源分类
     */
    @GetMapping("/ListStatisticsCheckType")
    public JSONObject ListStatisticsCheckType(TIndexData1 data1){
        JSONObject jo = new JSONObject();
        List<TIndexData1> list = iMessagePushService.statisticsCheckType(data1);
//        按照TIndexData1的dataSource分组得到Map
//        Map<String, List<TIndexData1>> map = list.stream().collect(Collectors.groupingBy(TIndexData1::getMouth));
//        Map<String, List<TIndexData1>> returnMap = new HashMap<>();
//        for (Map.Entry<String, List<TIndexData1>> entry : map.entrySet()) {
//            if("1".equals(entry.getKey())){
//                returnMap.put("dataSource1",entry.getValue());
//            }else if("2".equals(entry.getKey())) {
//                returnMap.put("dataSource2",entry.getValue());
//            }else  if("3".equals(entry.getKey())) {
//                returnMap.put("dataSource3",entry.getValue());
//            }else if("4".equals(entry.getKey())) {
//                returnMap.put("dataSource4",entry.getValue());
//            }else if("5".equals(entry.getKey())) {
//                returnMap.put("dataSource5",entry.getValue());
//            }else if("6".equals(entry.getKey())) {
//                returnMap.put("dataSource6",entry.getValue());
//            }else if("7".equals(entry.getKey())) {
//                returnMap.put("dataSource7",entry.getValue());
//            }else if("8".equals(entry.getKey())) {
//                returnMap.put("dataSource8",entry.getValue());
//            }else if("9".equals(entry.getKey())) {
//                returnMap.put("dataSource9",entry.getValue());
//            }
//        }
        jo.put("map",list);
        return jo;
    }

    /**
     * 统计问题等级
     */
    @GetMapping("/ListStatisticsQuestionLevel")
    public JSONObject ListStatisticsQuestionLevel(TIndexData1 data){
        JSONObject jo = new JSONObject();
        List<TIndexData1> list = iMessagePushService.statisticsQuestionLevel(data);
//        按照TIndexData1的dataSource分组得到Map
//        Map<String, List<TIndexData1>> map = list.stream().collect(Collectors.groupingBy(TIndexData1::getMouth));
//        Map<String, List<TIndexData1>> returnMap = new HashMap<>();
//        for (Map.Entry<String, List<TIndexData1>> entry : map.entrySet()) {
//            if("1".equals(entry.getKey())){
//                returnMap.put("level1",entry.getValue());
//            }else if("2".equals(entry.getKey())){
//                returnMap.put("level2",entry.getValue());
//            }else if("3".equals(entry.getKey())){
//                returnMap.put("level3",entry.getValue());
//            }
//        }
        jo.put("map",list);
        return jo;
    }

    /**
     * 质量问题排查治理情况
     */
    @GetMapping("/statisticsSituation")
    public JSONObject statisticsSituation(TIndexData2 data){
        JSONObject jo = new JSONObject();
        TIndexData2 projectInfo = new TIndexData2();
        TIndexData2 officeInfo = new TIndexData2();
        /*项目检查统计*/
        List<TIndexData2> data2 = iMessagePushService.statisticsSituation(data);
        int questionCounts = data2.stream().collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        int bigQuestionCounts = data2.stream().filter(tIndexData2 -> tIndexData2.getQuestionLevel().equals("2")).collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        int finishCounts = data2.stream().filter(tIndexData2 -> tIndexData2.getDataStatus().equals("2")).collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        projectInfo.setCheckCounts(String.valueOf(questionCounts)==""?null:String.valueOf(questionCounts));
        projectInfo.setQuestionCounts(String.valueOf(data2.size())==""?null:String.valueOf(data2.size()));
        projectInfo.setBigQuestionCounts(String.valueOf(bigQuestionCounts)==""?null:String.valueOf(bigQuestionCounts));
        if(data2.size()>0)
        projectInfo.setFinishCounts(new BigDecimal((double)finishCounts/data2.size()).setScale(2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))+"%");
        jo.put("projectInfo",projectInfo);
        /*企业检查统计*/
        data2 = iMessagePushService.statisticsSituation1(data);
        questionCounts = data2.stream().collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        bigQuestionCounts = data2.stream().filter(tIndexData2 -> tIndexData2.getQuestionLevel().equals("2")).collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        finishCounts = data2.stream().filter(tIndexData2 -> tIndexData2.getDataStatus().equals("2")).collect(Collectors.groupingBy(TIndexData2::getFileId)).size();
        officeInfo.setCheckCounts(String.valueOf(questionCounts)==""?null:String.valueOf(questionCounts));
        officeInfo.setQuestionCounts(String.valueOf(data2.size())==""?null:String.valueOf(data2.size()));
        officeInfo.setBigQuestionCounts(String.valueOf(bigQuestionCounts)==""?null:String.valueOf(bigQuestionCounts));
        if(data2.size()>0)
        officeInfo.setFinishCounts(new BigDecimal((double)finishCounts/data2.size()).setScale(2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))+"%");
        jo.put("officeInfo",officeInfo);
        return jo;
    }

}
